package com.study.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.study.mapper.LogMapper;
import com.study.mapper.RoleMapper;
import com.study.mapper.RoleResourcesMapper;
import com.study.model.Logs;
import com.study.model.RoleList;
import com.study.util.MyDate;
import com.study.util.getUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.model.Role;
import com.study.model.RoleResources;
import com.study.service.RoleService;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

@Service("roleService")
public class RoleServiceImpl extends BaseService<Role> implements RoleService{

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleResourcesMapper roleResourcesMapper;

    @Autowired
    private LogMapper logMapper;

    @Override
    public List<Role> queryRoleListWithSelected(Integer uid) {
        return roleMapper.queryRoleListWithSelected(uid);
    }

    @Override
    public PageInfo<Role> selectByPage(Role role, int start, int length) {
        int page = start/length+1;
        Example example = new Example(Role.class);
        //分页查询
        PageHelper.startPage(page, length);
        List<Role> rolesList = selectByExample(example);

//        //记录日志
//        getUserInfo getUserInfo = new getUserInfo(); //用户信息
//        MyDate myDate = new MyDate();//当前系统时间
//        Logs logs = new Logs();
//        logs.setLogDesc("用户:" + getUserInfo.getUserName()+"\t查看了角色列表");
//        logs.setCreateTime(myDate.getDate());
//        logMapper.addLog(logs);

        return new PageInfo<>(rolesList);
    }


   //列表
    @Override
    public PageInfo<RoleList> queryRoleInfo(Role role, int start, int length) {
        int page = start/length+1;
        PageHelper.startPage(page, length);
        List<RoleList> roleLists = roleMapper.queryRoleInfo();
        return new PageInfo<>(roleLists);
    }




    @Override
    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    public void delRole(Integer roleid) {
        //记录日志
        getUserInfo getUserInfo = new getUserInfo(); //登录用户信息
        MyDate myDate = new MyDate();//当前系统时间
        Logs logs = new Logs();
        //获取操作用户名
        String roleName = roleMapper.queryRoleName(roleid);
        logs.setLogDesc("用户:" + getUserInfo.getUserName()+"\t删除了id为" +roleid +"的角色--->" + roleName);
        logs.setCreateTime(myDate.getDate());
        //删除角色
        roleMapper.deleteRoleById(roleid);
        //写入日志
        logMapper.addLog(logs);
        //记录日志
        logs.setLogDesc("用户:" + getUserInfo.getUserName()+"\t删除了角色" +roleName +"的所有权限");
        logs.setCreateTime(myDate.getDate());
        //删除角色对应的资源表数据
        roleResourcesMapper.deleteRoleResources(roleid);
        logMapper.addLog(logs);
    }
}
